<template>
  <div class="top-box">
    <div style="cursor: pointer" class="fs-3" @click="router.push('/news/manage')">&lt;</div>
    <div class="fs-3">{{ messageInfo.title }}</div>
    <div>
      <HuUserlnfo></HuUserlnfo>
    </div>
  </div>
  <div class="messageInfo">
    <div v-if="messageInfo.mid">
      <div v-html="messageInfo.info"></div>
      <div>
        {{ Tools.formatDate(messageInfo.lastupdate, 'yyyy-MM-dd') }} - 点击量:{{
          messageInfo.hits
        }}
        点赞量:{{ messageInfo.praise }} 回复数:{{ messageInfo.reply }}
      </div>
    </div>
    <div v-else>
      <NEmpty description="留言信息加载中。。。"></NEmpty>
    </div>
    <!-- <div v-html="messageInfo.exInfo"></div> -->

    <div>
      <NModal preset="dialog" v-model:show="visible" title="举报">
        <div>
          <NInput type="textarea" v-model:value="info" placeholder="举报内容"></NInput>
        </div>
      </NModal>
    </div>
    <!-- 举报 -->
    <div>
      <NButton type="primary" @click="showAbbo">举报</NButton>
    </div>
    <NModal preset="dialog" v-model:show="visibl" title="举报">
      <div>
        <NInput type="textarea" v-model:value="infe" placeholder="举报内容"></NInput>
      </div>
      <template #action>
        <div>
          <NButton type="error" @click="submit">举报</NButton>
        </div>
      </template>
    </NModal>
  </div>

  <NDivider dashed>评论区</NDivider>
  <div v-if="list.length == 0">
    <NEmpty description="暂无评论"></NEmpty>
    <div class="tc p-2">
      <NButton type="primary" @click="showAdd">抢先发布</NButton>
    </div>
  </div>

  <div class="ccc">
    <div class="tc p-2">
      <NButton @click="showAdd" type="primary">发布评论</NButton>
    </div>
    <div v-for="d in list" :key="d.mid">
      <div class="content">
        <div>
          <NAvatar round :src="Api.userlogo(d.uid)"></NAvatar>
        </div>
        <div>
          <div>
            {{ d.user.info.nickname || d.user.user.username }}
            {{ Tools.formatDate(d.lastupdate, 'yyyy-MM-dd') }}
            -
            <span @click="reply(d.mrid)" :class="{ active: d.exInfo.praised }"
              >赞({{ d.praise }})</span
            >
            -
            <span v-if="d.exInfo.mine" @click="delReply(d.mrid)">删除</span>
            -
            <span></span>
            <div>
              <span>{{ d.info }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 评论 -->
  <div>
    <NModal preset="dialog" v-model:show="visible" title="发布评论">
      <div>
        <NInput type="textarea" v-model:value="info" placeholder="评论内容"></NInput>
      </div>
      <template #action>
        <div>
          <NButton type="primary" @click="addReply">发布</NButton>
        </div>
      </template>
    </NModal>
  </div>
</template>

<script setup lang="ts">
import { Api } from '@/types/Api'
import type { BaseDataResult, BaseResult, PageBean, PageVO } from '@/types/BaseResult'
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { NAvatar, NButton, NDivider, NEmpty, NInput, NModal, useMessage } from 'naive-ui'
import HuUserlnfo from '@/components/HuUserlnfo.vue'
import { Tools } from '@/types/Tools'
import type { TbMessage, TbMessageReply } from '@/types/message'
const message = useMessage()

const route = useRoute()
const router = useRouter()
const queryInfo = ref({ order: 1, info: '' })

const mid = route.params.id
console.log('留言id:', mid)

const loading = ref(false)

//#region  留言详情
const messageInfo = ref({} as TbMessage)

const query = () => {
  loading.value = true
  Api.get(`/message/${mid}`, {}, (data: BaseDataResult<TbMessage>) => {
    loading.value = false
    if (data.success) {
      messageInfo.value = data.data
      queryReply()
      return
    }
    message.error(data.message)
  })
}
// #endregion

// #region 留言板评论
const page = ref({ pageNumber: 1, pageSize: 10 } as PageBean)
const list = ref([] as Array<TbMessageReply>)

const queryReply = () => {
  loading.value = true
  Api.get(
    `/message/reply/${mid}/${page.value.pageSize}/${page.value.pageNumber}/${queryInfo.value.order}`,
    {},
    (data: BaseDataResult<PageVO<TbMessageReply>>) => {
      loading.value = false
      if (data.success) {
        list.value = data.data.list
        page.value = data.data.pageBean
        return
      }
      message.error(data.message)
    },
  )
}

// #endregion

// #region 举报
const visibl = ref(false)
const infe = ref('')

const showAbbo = () => {
  visibl.value = true
  infe.value = ''
}

const submit = () => {
  loading.value = true
  Api.post('/report/message', { mid: mid, info: infe.value }, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      visibl.value = false
      message.success(data.message)
    } else {
      message.error(data.message)
    }
  })
}

// #region 留言板评论
const visible = ref(false)
const info = ref('')
const showAdd = () => {
  info.value = ''
  visible.value = true
}

const addReply = () => {
  loading.value = true
  Api.post('/reply', { info: info.value, mid: mid }, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      visible.value = false
      message.success(data.message)
    } else {
      message.error(data.message)
    }
  })
}

// 删除
const delReply = (mrid: number) => {
  loading.value = true
  Api.delete(`/reply/${mrid}`, {}, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      message.success('删除成功')
      queryReply()
      return
    }
    message.error(data.message)
  })
}

// #endregion

//点赞删除
const reply = (mrid: number) => {
  loading.value = true
  Api.post(`/reply/praise/${mrid}`, {}, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      message.success(data.message)
      queryReply()
      return
    }
    message.error(data.message)
  })
}

onMounted(() => {
  query()
})
</script>

<style scoped lang="scss">
.top-box {
  display: flex;
  padding: 1rem;
  justify-content: space-between;
}
.messageInfo {
  width: 50vw;
  margin: 0px auto;
  :deep(p) {
    text-indent: 2rem;
    font-size: 1rem;
  }
  > div:first-of-type {
    font-size: 1.2rem;
  }
}
.ccc {
  width: 50vw;
  margin: 0px auto;
  > div {
    margin-bottom: 0.5rem;
  }
  .content {
    display: flex;
    > div:last-of-type {
      flex: 1;
      margin-left: 0.5rem;
    }
    .active {
      color: #ff0000;
    }
  }
}
</style>
